<?php

include_once("interface.php");

class Route extends Data
{
    private $fn = "/etc/sysconfig/static-routes";
    public $title = "Static Route Configuration";
    public $module = "route";
    public $data;

    public function __construct() {
        $this->data = array();
    }

    //read the data from files and return a complete object
    function readDataFromSystem() {

        if (!file_exists($this->fn)) {
            $tmp = fopen($this->fn, "w");
        }

        $filearray = file($this->fn);
        if ( $filearray == false ) {
            return false;
        }

        foreach ( $filearray as $arrval ) {
            $rt = array();
            #reads this e.g.
            #any net 172.16.55.0 netmask 255.255.255.0 gw 10.1.0.117
            list($junk1, $junk2, $net, $junk3, $mask, $junk4, $gw) = split(" ", $arrval);
            $rt['network'] = trim($net);
            $rt['mask'] = trim($mask);
            $rt['gateway'] = trim($gw);
            array_push($this->data, $rt);
        }

        error_log("Route readData(): read from $this->fn");
        return $this->data;
    }

    //validate the data on the server side
    function validateOnServer($obj) {
        return null;
    }

    //write the obj to the file
    function commitDataToSystem($obj) {
        $ret = 0;
        $retstr = "Successfully set static routes";

        $vstr = $this->validateOnServer($obj);
        if($vstr) {
            return "\"$vstr\"";
        }

        error_log("Route commitDataToSystem(): writing to $this->fn");
        $fp = fopen($this->fn, 'w');
        if($fp) {
            foreach ( $obj as $o ) {
                #any net 172.16.55.0 netmask 255.255.255.0 gw 10.1.0.117
                $str = "any net ".$o['network']." netmask ".$o['mask']." gw ".$o['gateway']."\n";
                $ret = fwrite($fp, $str);
                if($ret === false) {
                    $retstr = "Cannot set static routes. Failed to write to ".$this->fn;
                    return $retstr;
                }
            }
            fclose($fp);

            exec("/etc/init.d/network restart", &$out, &$ret);
            if($ret != 0) {
                $retstr = "Cannot set static routes. Failed to add route";
                error_log("Failed to set route $str\n");
            }
        }
        else {
            $retstr = "Cannot set static routes. Failed to open ".$this->fn;
        }

        return $retstr;
    }

    function printModuleHTML() {
        $str = <<<END

    <form>
    <fieldset class="ui-widget-content">
    <legend>Static Route</legend>
    <b style="margin-right: 7%;">Network</b>
    <b style="margin-right: 10%;">Mask</b><b>Gateway</b><br>

    <input id="network0" type=text size=15 />&nbsp;/
    <input id="mask0" type=text size=15 style="margin-right: 5%;"/><input id="gateway0" type=text size=15 /><br>

    <input id="network1" type=text size=15 />&nbsp;/
    <input id="mask1" type=text size=15 style="margin-right: 5%;"/><input id="gateway1" type=text size=15 /><br>

    <input id="network2" type=text size=15 />&nbsp;/
    <input id="mask2" type=text size=15 style="margin-right: 5%;"/><input id="gateway2" type=text size=15 /><br>

    <input id="network3" type=text size=15 />&nbsp;/
    <input id="mask3" type=text size=15 style="margin-right: 5%;"/><input id="gateway3" type=text size=15 /><br>

    <input id="network4" type=text size=15 />&nbsp;/
    <input id="mask4" type=text size=15 style="margin-right: 5%;"/><input id="gateway4" type=text size=15 /><br>

    <input type="button" id="submit" value="Submit"/>
    </fieldset>
    </form>
    <div id="info"></div>

END;

        return $str;
    }

    function printModuleScript() {
        $str = <<<END

<script type="text/javascript">
    //fill in the form using values received from server
    function writeDataToHTML(data) {
        for ( i = 0; i < data.length; i++ ) {
            var id_n = "#network" + i;
            var id_m = "#mask" + i;
            var id_g = "#gateway" + i;
            $(id_n).val(data[i].network);
            $(id_m).val(data[i].mask);
            $(id_g).val(data[i].gateway);
        }
    }

    //fill in the obj to be sent to the server using the form values
    function readDataFromHTML() {
        var data = Array();
        for ( i = 0; i < 5; i++ ) {
            var rt = Object();
            var id_n = "#network" + i;
            var id_m = "#mask" + i;
            var id_g = "#gateway" + i;
            rt.network = $(id_n).val();
            rt.mask = $(id_m).val();
            rt.gateway = $(id_g).val();

            if ( (rt.network === "")&& (rt.mask === "")&& (rt.gateway === "") ) {
                continue;
            }
            data.push(rt);
        }
        return data;
    }

    function validate(obj) {
        var msg = null;
        if( (obj.mask < 0) || (obj.mask > 32) ) {
            msg = "Please select a correct mask 0-32";
        }
        return msg;
    }

    //called before the submit operation
    function beforeSendFunction(msg) {
        $("#info").html("Sending ... ");
    }

    //called when the submit operation succeeds
    function successFunction(msg) {
        $("#submit").html("Submit");
        $("#info").html("Success: " + msg);
    }

    //called when the submit operation fails
    function errorFunction(msg) {
        $("#info").html("Error: " + msg);
    }

</script>

END;
        return $str;
    }
}

?>
